package cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH;

import android.content.Intent;
import android.util.Log;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.pendingTextInfo.PendingTextNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.staticPatientInfo.StaticPatientNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.staticPatientInfo.StaticSendBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticSendIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.send.StaticSendDetailActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/16.
 */

public class StaticSendIndexPresenter extends BasePresenter<StaticSendIndexViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;

    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);

    }
    public void quey(String CXDJLX, final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
                disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getContent("PDAQ09",CXDJLX,DJJLID)))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                            @Override
                            protected void onStart() {
                                if(null == waitDialog){
                                    waitDialog = CreateDialog.waitingDialog(activity);
                                }
                            }
                            @Override
                            public void onNext(RoleInfoResponseEnvelope xmlData) {
                                CreateDialog.dismiss(activity);
                                QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
                                if( null != queryBeen){
                                    if(Contants.HOSPITAL_STATIC_CHECK.equals(queryBeen.getDJDQZT())){
                                        //TODO: 判断是口服还是针剂药房的
                                        loadData(DJJLID);
                                        return;
                                    }
                                    switch (queryBeen.getDJDQZT()){
                                        case Contants.HOSPITAL_STATIC_TAKE:
                                            CreateDialog.failDialog3(activity,"无法进行送药","此取药单当前状态为已取,请重新确认");
                                            break;
                                        case Contants.FIRST:
                                            CreateDialog.failDialog3(activity,"无法进行送药","此取药单当前状态为未取,请重新确认");
                                            break;
                                        case Contants.SEND:
                                            CreateDialog.failDialog3(activity,"无法进行送药","此取药单当前状态为已送出,请重新确认");
                                            break;
                                        case Contants.RECEIVER:
                                            CreateDialog.failDialog3(activity,"无法进行送药","此取药单当前状态为已接收,请重新确认");
                                            break;
                                        case Contants.ERROR:
                                            CreateDialog.failDialog3(activity,"无法进行送药","此取药单当前状态为异常,请重新确认");
                                            break;
                                    }
                                }
                            }
                            @Override
                            public void onComplete() {
                            }
                            @Override
                            public void onError(Throwable e) {
                                CreateDialog.dismiss(activity);
                                if(null != e && null != e.getMessage()){
                                    Log.e("Throwable", e.getMessage());
                                    new MyToast(activity).showinfo(e.getMessage());
                                }
                            }
                        });
                addDisposable(disposable);
            }
    public void loadData(final String DJJLID) {
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(StaticPatientNode.getSendContent("PDAQ13", DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                        waitDialog.dismiss();
                        //TODO:住院信息获取失败 不跳转并提示重新扫码
//                         Log.e(TAG, "onNext: "+ xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult);
                        if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            new MyToast(activity).showinfo("信息获取失败，请重新扫码");
                            return;
                        }
                        //TODO:住院信息获取正确 跳转并传值
                        StaticSendBeen staticSendBeen = XmlUtils.getStaticSendBeen(xmlData.body.roleInfoResponse.retXml);
                        Intent intent = new Intent(activity, StaticSendDetailActivity.class);
                        intent.putExtra("inHospitalPatientBeenList",staticSendBeen);
                        intent.putExtra("DJJLID", DJJLID);
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
        //TODO:数据库存放 ??


        //TODO:扫码获取处方号码 跳转界面

    }

    /**
     * 1 门诊当日未取药处方数
     2 住院取完药 待审核数量
     * @param CXFSLX
     */
    public void setPendingNum(String CXFSLX){
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(PendingTextNode.getContent("PDAQ07",CXFSLX)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        //TODO:获取待完成订单
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
//                            new MyToast(activity).showinfo("未取药信息获取失败");
                            return;
                        }
                        //TODO:当日未取药处方数 / 住院取完药，待审核的数量     PDAQ07
                        String pendingNum =XmlUtils.getPendingNum(xmlData.body.roleInfoResponse.retXml);
//                          Log.e(TAG, "pendingNum: "+ pendingNum);
                        mOnReceivePendingNum.onPending(pendingNum);
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    private OnReceivePendingNum mOnReceivePendingNum;
    public   interface OnReceivePendingNum{
        void onPending(String onPendingNum);
    }
    public void registerOnReceivePendingNum(OnReceivePendingNum onReceivePendingNum){
        this.mOnReceivePendingNum=onReceivePendingNum;
    }
}
